package com.jd.oneday.month3;

/*
 * 1.构造器练习
 * 编写两个类，TriAngle和TriAngleTest,其中TriAngle类中声明私有边长base和高height，同时声明公共方法访问私有变量，
 * 此外提供类必要构造器，另一个类中使用这些公共方法，计算三角形的面积
 *
 *2.单例设计模式练习
 * 饿汉式
 * 懒汉式
 *
 * 3.代理模式
 *
 * 4.工厂模式
 *
 * 5.递归
 *
 */
public class Day03 {
    public static void main(String[] args) {

        //  构造器
//        TriAngle triAngle = new TriAngle(2.0,5.3);
//        triAngle.setBase(1.2);
//        triAngle.setHeight(3.2);
//        System.out.println(triAngle.getBase()+"**"+triAngle.getHeight());

        //  单例模式  饿汉式
//        Bank instance1 = Bank.getInstance();
//        Bank instance2 = Bank.getInstance();
//        System.out.println(instance1 == instance2);

        //  懒汉式
//        Order instance3 = Order.getInstance();
//        Order instance4 = Order.getInstance();
//        System.out.println(instance4 == instance4);

        //  代理模式
//        Server server = new Server();
//        ProxyServer proxyServer = new ProxyServer(server);
//        proxyServer.browes();

        //  工厂模式
//        Car car1 = new AudiFactory().getCar();
//        car1.run();
//        Car car2 = new BYDFactory().getCar();
//        car2.run();

        //  递归求和
        Day03 day03 = new Day03();
        int sum = day03.getSum(4);
        System.out.println(sum);
    }

    public int getSum(int n) {
        if (n == 1) {
            return 1;
        } else {
            return n + getSum(n - 1);
        }
    }
}

//工厂接口
interface Car {
    void run();
}

//两个工厂
class Audi implements Car {

    @Override
    public void run() {
        System.out.println("奥迪在跑");
    }
}

class BYD implements Car {
    @Override
    public void run() {
        System.out.println("BYD在跑");
    }
}

//工厂接口
interface CarFactory {
    Car getCar();
}

//两个工厂类
class AudiFactory implements CarFactory {

    @Override
    public Car getCar() {
        return new Audi();
    }
}

class BYDFactory implements CarFactory {
    @Override
    public Car getCar() {
        return new BYD();
    }
}


//代理接口
interface NetWork {
    void browes();
}

// 被代理类
class Server implements NetWork {

    @Override
    public void browes() {
        System.out.println("我是真实的服务器");
    }
}

//代理类
class ProxyServer implements NetWork {

    private NetWork netWork;

    public ProxyServer(NetWork netWork) {
        this.netWork = netWork;
    }

    public void check() {
        System.out.println("联网之前检查工作");
    }

    @Override
    public void browes() {
        check();
        netWork.browes();
    }
}


class Order {

    //1.私有化构造器
    private Order() {

    }

    //2.声明当前类对象，没有初始化
    private static Order instance = null;

    public static Order getInstance() {
        if (instance == null) {
            instance = new Order();
        }
        return instance;
    }
}

class Bank {
    //1.私有化构造器
    private Bank() {

    }

    //2.内部创建类的对象
    private static Bank instance = new Bank();

    //3.提供静态公共方法。返回类的对象
    public static Bank getInstance() {
        return instance;
    }

}

class TriAngle {
    private double base;
    private double height;

    public TriAngle() {

    }

    public TriAngle(double b, double h) {
        base = b;
        height = h;
    }

    public double getBase() {
        return base;
    }

    public void setBase(double base) {
        this.base = base;
    }

    public double getHeight() {
        return height;
    }

    public void setHeight(double height) {
        this.height = height;
    }
}

class TriAngleTest {


}